Golang : Handle sub domain with Gin
Just a supplementary tutorial for the previous tutorial on how to deal with sub domain in Golang. In case you find it too troublesome like me to introduce NewServeMux just to handle sub domain, do check out Gin framework. Below is an example on how to handle sub domain with Gin framework in Golang.
This example is from previous tutorial on how to deal with query string with a minor modification.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/query", func(c *gin.Context) {
first := c.Request.URL.Query().Get("first")
c.String(http.StatusOK, "First is "+first+"\n")
second := c.Request.URL.Query().Get("second")
c.String(http.StatusOK, "Second is "+second+"\n")
})
router.Run("api.example.com:8080") // port 8080 is optional //<---- here!
}
See also : Golang : Listen and Serve on sub domain example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.4k Linux/MacOSX : How to symlink a file?
+18.8k Golang : Execute shell command
+15.1k Golang : Get checkbox or extract multipart form data value example
+12.1k Golang : HTTP response JSON encoded data
+13k Golang : Get user input until a command or receive a word to stop
+11.2k Golang : Display a text file line by line with line number example
+13.2k Golang : Get dimension(width and height) of image file
+6.2k Golang : Totalize or add-up an array or slice example
+14.8k Golang : How to get Unix file descriptor for console and file
+51.4k Golang : How to get time in milliseconds?
+30.8k Golang : How to convert(cast) string to IP address?
+18.2k Golang : Generate thumbnails from images